package com.laxcen.lib;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.apkfuns.logutils.LogUtils;
import com.laxcen.toydemo.BaseActivity;
import com.laxcen.toydemo.R;

import util.DefaultHandler;
import util.OnCustomViewDialogCallback;
import util.ScreenUtil;

/**
 * Created by Ning6700 on 2017-06-05.
 */
public class CustomViewDialog extends Dialog implements DialogInterface.OnKeyListener {

    public Context customViewContext = null;
    public View view = null;
    public Object[] dialogParameters = null;
    public BaseActivity currentActivity;
    public ImageView ibtnDialogClose;


    public CustomViewDialog(Context context) {
        super(context, R.style.transparentDialogStyle);
        this.customViewContext = context;
        currentActivity = (BaseActivity) context;
    }

    public CustomViewDialog(Context context, int theme) {
        super(context, theme);
        this.customViewContext = context;
        currentActivity = (BaseActivity) context;
    }

    public Context getCustomViewContext() {
        return customViewContext;
    }

    private int layoutViewId;

    public void setLayoutViewId(int id) {
        this.layoutViewId = id;
    }

    protected int getLayoutViewId() {
        return layoutViewId;
    }

    @Override
    public void create() {
        LogUtils.d(CustomViewDialog.this.getClass().getName() + "--------create");
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
            super.create();
        }
        createCustomViewDialog();
        this.setCancelable(false);
        this.setCanceledOnTouchOutside(false);
        this.setOnKeyListener(this);
        //dialog_animation
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = getGravity(); // ####
        lp.width = getLayoutWidth();//WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = getLayoutHeight();//WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
        window.setWindowAnimations(R.style.dialog_animation);  //####

        initViewControls();
        new DefaultHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                initViewDatas();
            }
        }, 250);

    }


    private void resetHeight(int offsetHeight) {
        if (offsetHeight <= 0) return;
        int oldHeight = getLayoutHeight();
        int newHeight = oldHeight - offsetHeight;

        Window window = getWindow();
//        WindowManager.LayoutParams lp = window.getAttributes();
//        lp.gravity = getGravity(); // ####
//        lp.width = getLayoutWidth();//WindowManager.LayoutParams.MATCH_PARENT;
//        lp.height = newHeight;//WindowManager.LayoutParams.MATCH_PARENT;
//        window.setAttributes(lp);
        window.setLayout(getLayoutWidth(), newHeight);

        int maxOffset = ScreenUtil.dip2px(currentActivity, 60);
        if (offsetHeight > maxOffset) {
            dismiss();
        }
    }

    private void resetWindow() {
        int oldHeight = getLayoutHeight();

        Window window = getWindow();
        window.setLayout(getLayoutWidth(), oldHeight);
    }

    private int mLastX;
    private int mLastY;

    protected void initViewControls() {
        ibtnDialogClose = findViewById(R.id.ibtnDialogClose);
        if (ibtnDialogClose != null) ibtnDialogClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onDialogCloseButtonClicked();
            }
        });

    }

    protected void onDialogCloseButtonClicked() {
        cancel();
    }

    protected void initViewDatas() {
    }

    protected int getGravity() {
        return Gravity.CENTER;
    }

    protected int getLayoutWidth() {
        return WindowManager.LayoutParams.MATCH_PARENT;
    }

    protected int getLayoutHeight() {
        return WindowManager.LayoutParams.WRAP_CONTENT;
    }


    private void createCustomViewDialog() {
        view = View.inflate(this.customViewContext, getLayoutViewId(), null);
        this.setContentView(view);
    }

    //#############，#####，#####，#######，########activity########，######
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
//            this.dismiss();
//        }
        if (isCancelableOnBackKey == false)
            return false;
        else return true;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (onCustomViewDialogCallback != null)
            onCustomViewDialogCallback.onCallback(this, this.mResultStatus);
    }

    //#############，#####，#####，#######，########activity########，######
    private boolean isCancelableOnBackKey = true;

    /**
     * when set false, if click back button, will close dialog, default is true
     *
     * @param cancelableOnBackKey
     */
    public void setCancelableOnBackKey(boolean cancelableOnBackKey) {
        isCancelableOnBackKey = cancelableOnBackKey;
    }

    private int mResultStatus = EResultStatus.Failed;

    public int getResultStatus() {
        return mResultStatus;
    }

    public void setResultStatus(int mResultStatus) {
        this.mResultStatus = mResultStatus;
    }

    private OnCustomViewDialogCallback onCustomViewDialogCallback;

    public void setOnCustomViewDialogCallback(OnCustomViewDialogCallback onCustomViewDialogCallback) {
        this.onCustomViewDialogCallback = onCustomViewDialogCallback;
    }

    public static class EResultStatus {
        public static final int Successful = 1;
        public static final int Failed = 0;

        public static final int CollectionDeleted = 1000;
    }

}